<?php

namespace app\admin\controller;


use app\common\model\AppPatchModel;
use think\facade\Request;
use think\Validate;

class ApppatchController extends BaseController
{

    public function index()
    {
        return $this->fetch();
    }

    public function getlist()
    {
        $patch_id  = Request::param("patch_id");
        $page  = Request::param("page");
        $limit = Request::param("limit");
        $where = [];
        if(mb_strlen($patch_id,'UTF-8')>0){
            $where[] = ['patch_id','like',"%{$patch_id}%"];
        }
        $msgs  = AppPatchModel::with('admin')->where($where)->page($page, $limit)->order('id', 'desc')->select();
        if (count($msgs) > 0) {
            $count = AppPatchModel::count();
            return json(["code" => 0, "msg" => "", "data" => $msgs, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add()
    {
        return $this->fetch();
    }

    public function add_post()
    {
        $all_param = Request::param();

        $validate = new Validate([
            'base_code' => 'require|integer',
            'patch_url' => 'url',
            'patch_id'  => 'require|length:2,64',
        ], [
            "base_code" => "版本编号填写错误",
            "patch_url" => "app补丁下载地址错误",
            "patch_id"  => "补丁名称在2-64个字符",
        ]);

        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

        $app_patch = new AppPatchModel($all_param);

        //版本编号称是否占用
        $check = AppPatchModel::where(["base_code" => $app_patch->base_code])->find();
        if ($check) {
            return self::bulidFail("需要补丁版本编号已经存在，请更换");
        }

        //检补丁名称是否占用
        $check = AppPatchModel::where(["patch_id" => $app_patch->patch_id])->find();
        if ($check) {
            return self::bulidFail("补丁名称已经存在，请更换");
        }


        $app_patch->create_time = date("Y-m-d H:i:s");
        $app_patch->admin_id    = $this->admin_id;

        if ($app_patch->save()) {
            // 记录日志
            $log_content = "添加app版本升级补丁，ID：{$app_patch->id}，补丁名称：{$app_patch->patch_id}";
            parent::addAdminLog($log_content);
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function edit()
    {
        $id = Request::param('id');
        $app_patch = AppPatchModel::get($id);
        $this->assign('app_patch',$app_patch);
        return $this->fetch();
    }

    public function edit_post()
    {
        $all_param = Request::param();
        $id = Request::param('id');

        $validate = new Validate([
            'base_code' => 'require|integer',
            'patch_url' => 'url',
            'patch_id'  => 'require|length:2,64',
        ], [
            "base_code" => "版本编号填写错误",
            "patch_url" => "app补丁下载地址错误",
            "patch_id"  => "补丁名称在2-64个字符",
        ]);

        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

        $app_patch = AppPatchModel::get($id);
        if(!$app_patch){
            return self::bulidFail("修改的信息不存在");
        }

        //版本编号称是否占用
        $c_where = [
            ['id','<>',$id],
            ['base_code','=',$all_param['base_code']],
        ];
        $check = AppPatchModel::where($c_where)->find();
        if ($check) {
            return self::bulidFail("需要补丁版本编号已经存在，请更换");
        }

        //检测版本是否占用
        $c_where = [
            ['id','<>',$id],
            ['patch_id','=',$all_param['patch_id']],
        ];
        $check = AppPatchModel::where($c_where)->find();
        if ($check) {
            return self::bulidFail("补丁名称已经存在，请更换");
        }

        $app_patch->admin_id    = $this->admin_id;

        if ($app_patch->save($all_param)) {
            // 记录日志
            $log_content = "修改app版本升级补丁，ID：{$app_patch->id}，补丁名称：{$app_patch->patch_id}";
            parent::addAdminLog($log_content);
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    /**
     * 删除
     */
    public function del_post() {
        $id = Request::param('id', 0, 'intval');
        if ($id < 1) {
            return self::bulidFail('参数错误');
        }

        $app_patch = AppPatchModel::get($id);
        if (!$app_patch) {
            return self::bulidFail('信息不存在');
        }

        if ($app_patch::where(['id' => $id])->delete()) {
            // 记录日志
            $log_content = "删除app版本升级补丁，ID：{$app_patch->id}，补丁名称：{$app_patch->patch_id}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }
}